package com.hp.controller;

import com.hp.pojo.Template;
import com.hp.service.TemplateService;
import com.hp.util.PageResult;
import com.hp.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/template")
public class TemplateController {

    @Resource
    public TemplateService templateService;
    @RequestMapping("showIndex")
    public String showIndex(){
        return "template";
    }

    @RequestMapping("/findPage")
    @ResponseBody
    public PageResult<Template> findPage(int pageNum, int pageSize, @RequestBody Template template){
        PageResult pageResult = templateService.fingPage(pageNum, pageSize, template);
        return pageResult;

    }
    @RequestMapping("/findType")
    @ResponseBody
    public List<Template> findType(){
        return templateService.findType();
    }
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Template template){
        try {
            template.setSpecNum(0);
            template.setParaNum(0);
            templateService.add(template);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
    @PostMapping("/update")
    @ResponseBody
    public Result update(@RequestBody Template template){
        try {
            templateService.update(template);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
    @GetMapping("/findById")
    @ResponseBody
    public Template findById(int id){
        Template byId = templateService.findById(id);
        return byId;
    }

    @GetMapping("/delete")
    @ResponseBody
    public Result delete(int id){
        try {
            templateService.delete(id);
            return new Result();
        }catch (Exception e){
            e.printStackTrace();
            return new Result(1,"错误");
        }
    }
}
